﻿using System.Data;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Linq;

namespace FESSC_WORKBENCH_SERVER
{
    public class DB
    {
        private string server;
        private string user;
        private string password;
        private string database;
        public string connstr;

        public static DB getInstance()
        {
            return (System.Windows.Application.Current as App).db;
        }

        public DB(string server, string user, string password, string database)
        {
            this.server = server;
            this.user = user;
            this.password = password;
            this.database = database;
            var ecb = new EntityConnectionStringBuilder();
            var scb = new SqlConnectionStringBuilder();
            scb.DataSource = server;
            scb.InitialCatalog = database;
            scb.UserID = user;
            scb.Password = password;
            scb.IntegratedSecurity = true;
            connstr = scb.ToString();
            ecb.Provider = "System.Data.SqlClient";
            ecb.Metadata = @"res://*/Model_FESSC.csdl|res://*/Model_FESSC.ssdl|res://*/Model_FESSC.msl";
            ecb.ProviderConnectionString = connstr;
            connstr = ecb.ToString();
        }

        public FESSCEntities getEntities()
        {
            return new FESSCEntities(connstr);
        }

        public int select()
        {
            using (var context = getEntities())
            {
                var qry = context.Users;
                return qry.Count<Users>();
            }
        }
    }
}
